def singleton(cls):
    cls_dict = {}

    def __str__(self):
        return type(cls).__str__(cls)

    if hasattr(cls, '__str__'):
        cls.__str__ = __str__

    def get_instance(*args, **kwargs):
        if cls not in cls_dict:
            entity = cls(*args, **kwargs)
            cls_dict[cls] = entity
        return cls_dict[cls]

    return get_instance


if __name__ == '__main__':

    @singleton
    class Test:
        pass

    test1 = Test()
    test2 = Test()

    assert test1 is test2, "不是单例"

    print(type(test1))
    print(test1)
